#CODE DESCRIPTION: Uses 2008-2017 groundwater contour data from PV water to interpolate a raster of groundwater elevation.
#Converts land elevation data to feet, adds the land elevation to generate a raster of groundwater depth, and then assigns a value to each production well.


#import modules
import arcpy
import os

arcpy.CheckOutExtension("Spatial")
arcpy.CheckOutExtension("3D")
from arcpy.sa import *

#allows overwriting in geodatabase
arcpy.env.overwriteOutput = True

#set local variables
production_wells = r'C:\Users\Ellen\Dropbox\Pajaro_AgInnovation\Data\PV_Water_Metered_Wells.shp'

#set workspace path
gdb_path = r'C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb'

#set date
date = "20210526"

#convert land elevation DEM to feet to be in same units as contour lines--NOT NECESSARY 
#arcpy.gp.Times_sa("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_1arcsec_sc_mon_clip", "3.2808399", "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft")

#convert contour lines to raster
arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2008_fall.shp Id Contour",
                     "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2008")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2009_fall.shp CONTOUR Contour",
                     "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2009")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2010_fall.shp CONTOUR Contour",
                     "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2010")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2011_fall.shp CONTOUR Contour",
                     "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2011")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2012_fall.shp CONTOUR Contour",
                     "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2012")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2013_fall.shp CONTOUR Contour",
                     "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2013")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2014_fall.shp CONTOUR Contour",
                      "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2014")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2015_fall.shp Contour Contour",
                      "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2015")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2016_fall.shp CONTOUR Contour",
                      "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2016")

arcpy.TopoToRaster_3d("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/PV_Water_gw_cntrs_1947_2017/PV_Water_gw_cntrs_2017_fall.shp CONTOUR Contour",
                      "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2017")

arcpy.TopoToRaster_3d("C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/Raw_Data/PV_Water_Bruno_E_20210427_Data_Request.gdb/gw_cntrs_2018_fall CONTOUR Contour",
                      "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2018")

arcpy.TopoToRaster_3d("C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/Raw_Data/PV_Water_Bruno_E_20210427_Data_Request.gdb/gw_cntrs_2019_fall CONTOUR Contour",
                      "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2019")

arcpy.TopoToRaster_3d("C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/Raw_Data/PV_Water_Bruno_E_20210427_Data_Request.gdb/gw_cntrs_2020_fall CONTOUR Contour",
                      "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2020")

#Need to subtract gw elevation from land elevation
arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2008"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2008")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2009"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2009")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2010"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2010")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2011"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2011")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2012"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2012")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2013"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2013")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2014"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2014")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2015"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2015")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2016"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2016")

arcpy.gp.RasterCalculator_sa('"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2017"',
                            "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2017")

arcpy.gp.RasterCalculator_sa('"C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2018"',
                             "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2018")

arcpy.gp.RasterCalculator_sa('"C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2019"',
                             "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2019")

arcpy.gp.RasterCalculator_sa('"C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/dem_sc_mon_clip_ft" - "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_elevation_2020"',
                             "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2020")

#extract by points: assign a depth to each production well

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2008",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2008", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2009",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2009", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2010",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2010", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2011",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2011", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2012",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2012", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2013",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2013", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2014",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2014", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2015",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2015", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2016",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2016", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2017",
                               "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2017", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2018",
                                "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2018", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2019",
                                "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2019", "INTERPOLATE", "ALL")

arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_2020",
                                "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2020", "INTERPOLATE", "ALL")


#export attribute table to excel file
arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2008",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2008_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2009",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2009_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2010",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2010_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2011",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2011_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2012",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2012_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2013",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2013_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2014",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2014_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2015",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2015_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2016",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2016_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")

arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2017",
                             "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2017_20190326.xls",
                             Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")


arcpy.TableToExcel_conversion("C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2018",
                              "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2018_20210526.xls",
                              Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")


arcpy.TableToExcel_conversion("C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2019",
                              "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2019_20210526.xls",
                              Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")


arcpy.TableToExcel_conversion("C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_2020",
                              "C:/Users/Ellen/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_2020_20210526.xls",
                              Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")


##for y in range(2008, 2018):
##    arcpy.gp.ExtractValuesToPoints_sa(production_wells,"C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/gw_depth_" + str(y),
##                                "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_" + str(y), "INTERPOLATE", "ALL")
##    
##    arcpy.TableToExcel_conversion("C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/WorkingData.gdb/depth_to_wells_" + str(y),
##                              "C:/Users/ebruno/Dropbox/Pajaro_AgInnovation/Data/groundwater_depth_to_wells_" + str(y) + "_20190325.xls",
##                              Use_field_alias_as_column_header="NAME", Use_domain_and_subtype_description="CODE")
